package ar.com.larctest.forms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.adapters.ListViewMasterDataHorsesAdapter;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.Horses;
import ar.com.larctest.providers.HorsesProvider;

public class HorsesActivity extends Activity {
    private static final String TAG = "HorsesActivity";
    private static final int MY_CHILD_ACTIVITY = 998;

    private ListViewMasterDataHorsesAdapter horsesAdapter;
    private List<Horses> lstHorses = new ArrayList<Horses>();
    Horses selectedHorse;

    EditText etName;
    ListView listViewHorsesData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horses);

        listViewHorsesData = (ListView) findViewById(R.id.lvHorses);
        etName = (EditText) findViewById(R.id.etName);

        try
        {
            horsesAdapter = new ListViewMasterDataHorsesAdapter(HorsesActivity.this, R.id.lvHorses, lstHorses);
            View header = getLayoutInflater().inflate(R.layout.listview_header_horses, null);
            listViewHorsesData.addHeaderView(header);

            listViewHorsesData.setAdapter(horsesAdapter);
            listViewHorsesData.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    for(Horses horseItem: lstHorses)
                        horseItem.setSelected(false);

                    selectedHorse = lstHorses.get(position - 1);
                    selectedHorse.setSelected(true);
                    horsesAdapter.notifyDataSetChanged();
                }
            });
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    public void onClickSearch(View view)
    {
        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(this);
        sqlHelper.dbOpen();

        try
        {
            HorsesProvider horsesProvider = new HorsesProvider(this);
            lstHorses = horsesProvider.getHorses(etName.getText().toString());
            horsesAdapter.clear();
            horsesAdapter.notifyDataSetChanged();
            horsesAdapter = new ListViewMasterDataHorsesAdapter(HorsesActivity.this, R.id.lvHorses, lstHorses);
            listViewHorsesData.setAdapter(horsesAdapter);
            horsesAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
        finally {
            sqlHelper.dbClose();
        }
    }

    public void onClickDetail(View view)
    {
        if(selectedHorse == null) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) (layout != null ? layout.findViewById(R.id.text) : null);
            if (text != null) {
                text.setText(getText(R.string.Horses_HorseRequired));
            }

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else {
            Intent intentViewHorse = new Intent(this, HorsesDataActivity.class);
            intentViewHorse.putExtra("selectedHorse", selectedHorse.getIdHorse());
            startActivityForResult(intentViewHorse, 1);
        }
    }

    public void onClickAdd(View view)
    {
        Intent intentAddHorse = new Intent(this, PopUpHorseAddActivity.class);
        startActivityForResult(intentAddHorse, MY_CHILD_ACTIVITY);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode) {
            case (MY_CHILD_ACTIVITY) : {
                if (resultCode == Activity.RESULT_OK)
                    horsesAdapter.notifyDataSetChanged();
                break;
            }
        }
    }
}
